package com.jiexin.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author: Jay_Liu
 * @Description: 获取Bean对象
 * @Date: Created in 13:38 2018/4/2 0002
 * @Modified By:
 */
@Component("applicationContextHelper")
public class ApplicationContextHelper implements ApplicationContextAware{

    public static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    //根据类型获取Bean
    public static <T> T popBean(Class<T> clazz){
        if (applicationContext == null){
            return null;
        }
        return applicationContext.getBean(clazz);
    }

    //根据bean对象名称，其类型获取Bean
    public static <T> T popBean(String name, Class<T> clazz){
        if (applicationContext == null){
            return null;
        }
        return applicationContext.getBean(name, clazz);
    }
}
